home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 4.9 KB | 144 lines | [TEXT/MPS ] |
- /*
- File: TArrayExample.cp
-
- Contains: This module shows an example use of the TArray and TMatchObjects classes
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TInitSLM.h" // the TInitSLM class and SLM include files
- #include "TArrayExample.h"
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// CONSTANTS
- ///————————————————————————————————————————————————————————————————————————————————————
-
- const unsigned kTotalData = 10; // number of TData objects we'll add to the array
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// PROTOTYPES
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void build_data_array(TArray* dataArray );
- static void disp_data(TArray* dataArray );
- static void remove_data(TArray* dataArray );
-
- /*————————————————————————————————————————————————————————————————————————————————————
- main
-
- This example creates an array of TData objects by using the TArray class. It first
- create instances of TData, adding them to the array as they are created. Once
- the objects have been added, it goes thru the array and displays the data associated
- with each element. Next, it removes two elements from the array, one using an
- explicit index and another by using a match object. It then display the array
- a second time. Finally, it removes each remaining element from the array.
- —————————————————————————————————————————————————————————————————————*/
-
- main()
- {
- TInitSLM initLibraryManager; // initialize the shared library manager
-
- if( initLibraryManager.Failed() ) // If we failed, let's go home
- return 1;
-
- TArray *dataarray = new TArray(kTotalData);// allocate our TArray to add our data to
-
- if (dataarray && !dataarray->IsValid()) // make sure the TArray was constructed properly
- {
- delete dataarray;
- return 0;
- }
-
- if( dataarray ) { // Here we are going to create an array of data
-
- build_data_array( dataarray ); // build an array of data
-
- if( ! dataarray->IsEmpty() ) { // make sure the array is not empty
-
- cout << "Total data in the array = " << dataarray->Count() << endl;
-
- disp_data( dataarray ); // go ahead and display each element
-
- // now we are going to remove the 3rd element of the array
- TData* thedata = (TData*)(*dataarray)[2];
- dataarray->Remove( thedata );
- cout << "\nRemoved 3rd element from the array, data = " << thedata->GetData()
- << endl;
- delete thedata;
-
- // now we are going to remove the element containing a 7
- cout << "\nAbout to remove element with data = 7\n" << endl;
- TDataMatchObject match(7);
- thedata = (TData*)dataarray->Remove(match);
- cout << "\nRemoved element, data = " << thedata->GetData() << endl;
- delete thedata;
-
- cout << "\nNumber of elements in the array = " << dataarray->Count() << endl;
-
- // let's see now what we has happend to third location of array
- disp_data( dataarray ); // go ahead and display each element
- remove_data( dataarray ); // go ahead and clean up
- }
- else
- cout << "Array is empty.\n" << endl;
- delete dataarray; // we are done with the array
- }
-
- return 0;
- }
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// build_data_array
- ///
- /// build an array containing TData objects
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void build_data_array(TArray* dataArray )
- {
- cout << "\nBuilding the array of TData objects\n" << endl;
-
- for( short i=0; i< kTotalData; i++ ) {
- TData *tdata = new TData(i); // i will also be the value stored in the TData
- dataArray->Add( tdata );
- }
- }
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// disp_data
- ///
- /// index thru the array and display each element of the array
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void disp_data(TArray* dataArray )
- {
- size_t i = 0;
- TData *thedata;
-
- // let's index through our array, and display each TData object
-
- cout << "\nIndexing thru the array and displaying each element\n" << endl;
-
- while( thedata = (TData *)(*dataArray)[i] )
- cout << "array[" << i++ << "] = " << thedata->GetData() << endl;
- }
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// remove_data
- ///
- /// index thru the array and remove each TData object
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void remove_data(TArray* dataArray )
- {
- // We'll just keep on removing the first element of the array until there
- // are none left. If we didn't have to process each element as it was removed
- // we could have just called RemoveAll or DeleteAll.
- TData *thedata;
- while( thedata = (TData *)(*dataArray)[0] ) {
- dataArray->Remove( thedata );
- cout << "Removed element, data = " << thedata->GetData() << endl;
- delete thedata;
- }
- }
-